package com.douban.book.service;

import java.io.File;
import java.util.Set;

import com.douban.book.bean.Book;

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class CreateExcel {
	
	public  boolean insertData(Set<Book> set,String topic,String path) {
		boolean flag = true;
		File file = new File(path+"/"+topic+".xls");
		try {
			file.createNewFile();
			WritableWorkbook workbook = Workbook.createWorkbook(file);
			WritableSheet sheet = workbook.createSheet(topic, 0);
			String[] th = { "序号", "书名", "评分", "评价人数", "作者", "出版社", "出版日期", "价格"  };
			Label label = null;
			// �����ͷ
			for (int i = 0; i < th.length; i++) {
				label = new Label(i, 0, th[i]);
				sheet.addCell(label);
				sheet.setColumnView(1, 35);
				sheet.setColumnView(4, 35);
				sheet.setColumnView(5, 15);
			}
			// ��������
			int i = 0;
			for (Book book:set) {
				i++;
				sheet.setRowView(i, false);
				label = new Label(0, i, i + "");
				sheet.addCell(label);

				label = new Label(1, i, book.getBookName());
				sheet.addCell(label);

				label = new Label(2, i, book.getScore() + "");
				sheet.addCell(label);

				label = new Label(3, i, book.getPersonNum() + "");
				sheet.addCell(label);

				label = new Label(4, i, book.getAuthor());
				sheet.addCell(label);

				label = new Label(5, i, book.getPublish());
				sheet.addCell(label);

				label = new Label(6, i, book.getPublishDate());
				sheet.addCell(label);

				label = new Label(7, i, book.getPrice());
				sheet.addCell(label);
			}
			workbook.write();
			workbook.close();
		} catch (Exception e) {
			e.printStackTrace();
			flag = false;
		}
		return flag;
	}
}
